package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.ChargeRecord;
import com.kichina.parking.service.logic.service.ChargeRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 固定车收费后主动上传缴费信息业务处理层
 */
@Component
@Slf4j
public class Parking2MonthPayUpstreamHandler {

    private final ChargeRecordService chargeRecordService;

    public Parking2MonthPayUpstreamHandler(ChargeRecordService chargeRecordService) {
        this.chargeRecordService = chargeRecordService;
    }

    public void recordMonthPayUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        ChargeRecord chargeRecord;
        try {
            chargeRecord = JsonToEntityUtil.jsonToEntity(jsonObject, ChargeRecord.class);
            if (Objects.nonNull(jsonObject.getString("pay_time"))) {
                if(String.valueOf(jsonObject.getString("pay_time")).length()<15) {
                    //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                    chargeRecord.setPayTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("pay_time"))));
                }else {
                    chargeRecord.setPayTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("pay_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("start_time"))) {
                if(String.valueOf(jsonObject.getString("start_time")).length()<15) {
                    chargeRecord.setStartTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("start_time"))));
                }else {
                    chargeRecord.setStartTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("start_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("end_time"))) {
                if(String.valueOf(jsonObject.getString("end_time")).length()<15) {
                    chargeRecord.setEndTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("end_time"))));
                }else {
                    chargeRecord.setEndTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("end_time"))));
                }
            }

            LambdaQueryWrapper<ChargeRecord> eq = new QueryWrapper<ChargeRecord>().lambda()
                    .eq(ChargeRecord::getParkid, chargeRecord.getParkid())
                    .eq(ChargeRecord::getCarNumber, chargeRecord.getCarNumber());
            List<ChargeRecord> recordList = chargeRecordService.list(eq);
            if (CollectionUtils.isNotEmpty(recordList)){
                //更新
                chargeRecord.setUpdateTime(new Date());
                chargeRecordService.update(chargeRecord, eq);
            }else {
                //添加
                chargeRecord.setChargeRecordId(UUIDUtil.getUUID());
                chargeRecord.setUpdateTime(new Date());
                chargeRecord.setCreateTime(new Date());
                chargeRecordService.save(chargeRecord);
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
